import {
    SET_ALLPLAN,
    SET_PLAN_DETAIL,
    SET_PLUS_PLAN,
    SET_DELETE_CURRENT_PLAN,
    SET_PLAN
} from '../mutation-types';
import api from '../../api';

// import { errFun } from '../../tools.js'

const state = {
    plans: [],
    plan_detail: [],
    // plan:{},
    // today:null,
    // allday:null,
    plus_plan: {}
}

const mutations = {
    [SET_ALLPLAN](state, data) {
        state.plans = data;
        data.forEach(function(el, index) {
            state.plans[index].kcal = el.kcal.toFixed(1);
        })
    },
    //
    // [SET_PLAN_DETAIL](state, data) {
    //     state.plan_detail = data;
    //     state.plan_detail.kcal = data.kcal.toFixed(1);
    // },
    [SET_ALLPLAN](state, data) {
        //  console.log(data);
        state.plans = data;
        data.forEach(function(el, index) {
            state.plans[index].kcal = parseInt(el.kcal);
            state.plans[index].dynamicKcal = parseInt(el.dynamicKcal);
        })
    },

    [SET_PLAN_DETAIL](state, data) {

        state.plan_detail = data;
        state.plan_detail.kcal = data.kcal.toFixed(1);
        state.plan_detail.dynamicKcal = 0;
        data.courses.forEach(function(el, index) {
            state.plan_detail.courses[index].duration = el.duration > 60 ? parseInt(el.duration / 60) + '分钟' : el.duration + '秒';
            state.plan_detail.courses[index].dynamicKcal = parseInt(state.plan_detail.courses[index].dynamicKcal);
            state.plan_detail.dynamicKcal = state.plan_detail.dynamicKcal + state.plan_detail.courses[index].dynamicKcal;
        })
    },

    [SET_PLUS_PLAN](state, { data, status }) {
        state.plus_plan = data;
        state.plus_plan = {...state.plus, status: status };
    },
}

const actions = {
    //全部计划
    GET_ALLPLAN: ({ commit, dispatch }) => {
        //  dispatch('CHANGE_LOADING_SHOW').then(
        api.getAllPlan().then(response => {
                // 将热身计划提前；
                var temparrA = [],
                    temparrB = [];
                for (var i in response.data.result) {
                    if (response.data.result[i].id == 10 || response.data.result[i].id == 11) {
                        temparrA.push(response.data.result[i]);
                    } else {
                        temparrB.push(response.data.result[i])
                    }
                }
                response.data.result = temparrA.concat(temparrB)
                if (response.data.code == 100) {
                    commit(SET_ALLPLAN, response.data.result);
                }
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            //  )
    },


    //获取计划介绍
    GET_PLAN_DETAIL: ({ commit, dispatch }, id) => {
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.getPlanDetail(id).then(response => {
                //  debugger;
                // console.log(response.data);
                commit(SET_PLAN_DETAIL, response.data);
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },

    //删除当前计划
    DELETE_CURRENT_PLAN: ({ commit, dispatch }, id) => {
        // dispatch('CHANGE_LOADING_SHOW').then(
        api.delete_current_plan(id).then(response => {
                //  console.log(response.data);
                // if(response.status){
                //   choosed == false
                // }
                // commit(SET_DELETE_CURRENT_PLAN,response.data);
                // dispatch('HIDDEN_LOADING_SHOW')
            })
            // )
    },
}

export default {
    state,
    actions,
    mutations
}